package condominio.servlet.cuota;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import condominio.excepcion.DAOExcepcion;
import condominio.negocio.GestionCuotas;

/**
 * Servlet implementation class GenerarCuotaServlet
 */
@WebServlet("/GenerarCuotaServlet")
public class GenerarCuotaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GenerarCuotaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		int vivienda = Integer.parseInt(request.getParameter("vivienda"));
		int anno = Integer.parseInt(request.getParameter("anno"));
		int mes = Integer.parseInt(request.getParameter("mes"));
		String strFechaVencimiento = request.getParameter("fechavencimiento");
		double importe = Double.parseDouble(request.getParameter("importe"));
		
		//
		SimpleDateFormat parse1 = new SimpleDateFormat("dd-MM-yyyy");
		java.util.Date date=null;		
		
		try {
			date = parse1.parse(strFechaVencimiento);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		
		java.sql.Date fechaVencimiento = new java.sql.Date(date.getTime());
		
		
		String strfechaPago="01-01-1999";
		SimpleDateFormat parse2 = new SimpleDateFormat("dd-MM-yyyy");
		java.util.Date datefechaPago = null;
		try {
			datefechaPago = parse2.parse(strfechaPago);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		java.sql.Date fechaPago = new java.sql.Date(datefechaPago.getTime());
		
		
		GestionCuotas negocioGC = new GestionCuotas();
		try {
			negocioGC.insertar(anno, mes, vivienda, importe, fechaVencimiento, fechaPago, 0, 0);
			response.sendRedirect(request.getContextPath()	+ "/GenerarCuota.jsp");
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
		
	}

}
